import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:client/common/my_http.dart';
import 'package:client/view/reg.dart';
import '../model/user_model.dart';
import 'package:provider/provider.dart';

class DemoRoute extends StatelessWidget {
  const DemoRoute({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("demo"),
      ),
      body: _buildBody(context),
    );
  }

  static Future<Socket>? _conn;

  _buildBody(context) {
    _conn = Socket.connect("127.0.0.1", 20000);
    // _conn = Socket.connect("127.0.0.1", 2684);

    _conn?.then((Socket conn) {
      conn.listen((event) {
        print("event");
        print(event);
        var infoBody = event.skip(2);
        print(event[0]);
        print(event[1]);
        print(infoBody);
        var str = String.fromCharCodes(infoBody);
        print(str);
        str = const Utf8Decoder().convert(infoBody.toList());
        print(str);
        var str1 = utf8.decode(infoBody.toList());
        print(str1);
        print(json.decode(str1));
      });
    });

    var nameController = TextEditingController();
    var pwdController = TextEditingController();
    var loginView = Column(
      children: [
        TextField(
          autofocus: true,
          controller: nameController,
          decoration: const InputDecoration(
              labelText: "cmd", prefixIcon: Icon(Icons.person)),
        ),
        TextField(
          autofocus: true,
          controller: pwdController,
          decoration: const InputDecoration(
              labelText: "info", prefixIcon: Icon(Icons.password_sharp)),
        ),
        Container(
            padding: EdgeInsets.all(30),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(
                  onPressed: () async {
                    var cmd = nameController.text;
                    print(cmd);
                    var cmdCode = int.parse(cmd);
                    print(cmdCode);
                    var input = pwdController.text;
                    print(input);

                    var mapData = {"msg": input};
                    var sendStr = json.encode(mapData);

                    var builder = BytesBuilder();
                    var info = utf8.encode(sendStr);
                    print(info);
                    var len = info.length + 1;
                    print(len);
                    builder.add(Uint8List.fromList([len]));
                    builder.add(Uint8List.fromList([cmdCode]));
                    builder.add(info);
                    print("end");
                    var sendMsg = builder.toBytes();
                    print(sendMsg);

                    _conn?.then((Socket conn) {
                      conn.add(sendMsg);
                    });
                  },
                  child: Text("send"),
                )
              ],
            ))
      ],
    );
    return loginView;
  }
}
